﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rodunu.Business.Models.Movies;
using Rodunu.Services.IMDB;

namespace Rodunu.Clients.WPF.Collection
{
    /// <summary>
    /// Interaction logic for AddMovieToCollection.xaml
    /// </summary>
    public partial class AddMovieToCollection : UserControl
    {
        #region Enums

        private enum AddState
        {
            MovieInformation,
            Details,
            TechnicalInformation,
            Languages,
            Tags,
            Plot,
            Cast,
            Notes
        }

        #endregion
        
        #region Private Members
        
        private AddState _currentState;

        #endregion

        #region Private Members

        private readonly Controller _controller;
        private Title _newMovie;

        #endregion

        #region Constructors

        public AddMovieToCollection()
        {
            InitializeComponent();

            //TODO: pull services dynamically
            ServicesDropDown.Items.Add("IMDB");
            ServicesDropDown.Items.Add("Amazon");
            ServicesDropDown.Items.Add("Netflix");
            ServicesDropDown.SelectedValue = "IMDB";

            PullFromWebButton.Click += PullFromWebButton_Click;

            SetEvents();

            if (_controller == null)
                _controller = new Controller();

            if (_newMovie == null)
                _newMovie = new Title();

            _currentState = AddState.MovieInformation;
            HandleState();
        }

        #endregion

        void PullFromWebButton_Click(object sender, RoutedEventArgs e)
        {
            var searchText = "";
            if (SearchTextBox.Text.Length > 0)
                searchText = SearchTextBox.Text;
            else if (SearchTextBox.Text.Length > 0)
                searchText = SearchTextBox.Text;
            else
            {
                MessageBox.Show("Please enter either an original title or title to search against");
                return;
            }

            Cursor = Cursors.Wait;

            IMDBSearchResultsPane.Visibility = Visibility.Visible;
            AddCollectionPanels.Visibility = Visibility.Collapsed;
            IMDBSearchResultsViewer.GetSearchResults(searchText);

            Cursor = Cursors.Arrow;
        }

        #region Private Methods

        private void SetEvents()
        {
            SelectSearchResult.Click += SelectSearchResult_Click;
            CancelSearchResult.Click += CancelSearchResult_Click;

            MovieInformationButton.Click += MovieInformationButton_Click;
            DetailsButton.Click += DetailsButton_Click;
            CastButton.Click += CastButton_Click;
            LanguagesButton.Click += LanguagesButton_Click;
            NotesButton.Click += NotesButton_Click;
            PlotButton.Click += PlotButton_Click;
            TagsButton.Click += TagsButton_Click;
            TechnicalInformationButton.Click += TechnicalInformationButton_Click;
        }

        #region Handle Navigation

        #region Left Menu Button Events

        void TechnicalInformationButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.TechnicalInformation;
            HandleState();
        }

        void TagsButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.Tags;
            HandleState();
        }

        void PlotButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.Plot;
            HandleState();
        }

        void NotesButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.Notes;
            HandleState();
        }

        void LanguagesButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.Languages;
            HandleState();
        }

        void CastButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.Cast;
            HandleState();
        }

        void DetailsButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.Details;
            HandleState();
        }

        void MovieInformationButton_Click(object sender, RoutedEventArgs e)
        {
            _currentState = AddState.MovieInformation;
            HandleState();
        }

        #endregion

        private void HandleState()
        {
            EnableAllButtons();

            HideAllUserControls();

            switch (_currentState)
            {
                default:
                    MovieInformationButton.IsEnabled = false;
                    MovieInfoForm.Visibility = System.Windows.Visibility.Visible;
                    MovieInfoForm.Movie = _newMovie;
                    break;
                case AddState.Details:
                    DetailsButton.IsEnabled = false;
                    DetailsForms.Visibility = System.Windows.Visibility.Visible;
                    break;
                case AddState.Cast:
                    CastButton.IsEnabled = false;
                    CastForm.Visibility = System.Windows.Visibility.Visible;
                    break;
                case AddState.Languages:
                    LanguagesButton.IsEnabled = false;
                    LanguagesForm.Visibility = System.Windows.Visibility.Visible;
                    break;
                case AddState.Notes:
                    NotesButton.IsEnabled = false;
                    NotesForm.Visibility = System.Windows.Visibility.Visible;
                    break;
                case AddState.Plot:
                    PlotButton.IsEnabled = false;
                    PlotForm.Visibility = System.Windows.Visibility.Visible;
                    PlotForm.Movie = _newMovie;
                    break;
                case AddState.Tags:
                    TagsButton.IsEnabled = false;
                    TagsForm.Visibility = System.Windows.Visibility.Visible;
                    break;
                case AddState.TechnicalInformation:
                    TechnicalInformationButton.IsEnabled = false;
                    TechnicalInformationForm.Visibility = Visibility.Visible;
                    break;
            }
        }

        private void HideAllUserControls()
        {
            MovieInfoForm.Visibility = System.Windows.Visibility.Collapsed;
            DetailsForms.Visibility = System.Windows.Visibility.Collapsed;
            TechnicalInformationForm.Visibility = Visibility.Collapsed;
            TagsForm.Visibility = System.Windows.Visibility.Collapsed;
            LanguagesForm.Visibility = System.Windows.Visibility.Collapsed;
            CastForm.Visibility = System.Windows.Visibility.Collapsed;
            NotesForm.Visibility = System.Windows.Visibility.Collapsed;
            PlotForm.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void EnableAllButtons()
        {
            MovieInformationButton.IsEnabled = true;
            DetailsButton.IsEnabled = true;
            CastButton.IsEnabled = true;
            LanguagesButton.IsEnabled = true;
            NotesButton.IsEnabled = true;
            PlotButton.IsEnabled = true;
            TagsButton.IsEnabled = true;
            TechnicalInformationButton.IsEnabled = true;
        }

        #endregion
        
        #endregion

        #region Movie Search

        void CancelSearchResult_Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;
            IMDBSearchResultsPane.Visibility = Visibility.Collapsed;
            AddCollectionPanels.Visibility = Visibility.Visible;
            Cursor = Cursors.Arrow;
        }

        void SelectSearchResult_Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;

            IMDBSearchResultsPane.Visibility = Visibility.Collapsed;
            AddCollectionPanels.Visibility = Visibility.Visible;
            string id = IMDBSearchResultsViewer.GetSelectedMovie();
            Title IMDBMovie = _controller.GetTitle(id);

            //Check for movie and save it local
            //if (_newMovie.ImageUrl.Length == 0 && IMDBMovie.ImageUrl.Length > 0)
            //{

            //}

            if (OverwriteFilledDataCheckBox.IsChecked == true)
                _newMovie = IMDBMovie;
            else
                _newMovie = DataHelper.FillTitleWithOutOverwrite(IMDBMovie, _newMovie);
            MovieInfoForm.Movie = _newMovie;
            DetailsForms.Movie = _newMovie;
            PlotForm.Movie = _newMovie;
            TechnicalInformationForm.Movie = _newMovie;
            CastForm.Movie = _newMovie;

            Cursor = Cursors.Arrow;
        }

        #endregion

        public Title GetNewMovie()
        {
            //TODO: pull in from user controls
            return _newMovie;
        }
    }
}
